namespace AonesoftBase.Biz
{
    using System;
    using System.Data;
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI;

    public class PrintManager
    {
        public static void ToExcel(DataTable dt)
        {
            string str = "";
            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    str = str + row[i].ToString() + "\t";
                }
                str = str + "\n";
            }
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=myexcel.xls");
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = Encoding.Default;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            StringWriter writer = new StringWriter();
            HtmlTextWriter writer2 = new HtmlTextWriter(writer);
            writer2.WriteLine(str.ToString());
            HttpContext.Current.Response.Write(writer.ToString());
            HttpContext.Current.Response.End();
            writer2.Flush();
            writer2.Close();
            writer.Flush();
            writer.Close();
        }

        public static void ToExcel(string sb)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "inline;filename=myexcel.xls");
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = Encoding.Default;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            StringWriter writer = new StringWriter();
            HtmlTextWriter writer2 = new HtmlTextWriter(writer);
            writer2.WriteLine(sb.ToString());
            HttpContext.Current.Response.Write(writer.ToString());
            HttpContext.Current.Response.End();
            writer2.Flush();
            writer2.Close();
            writer.Flush();
            writer.Close();
        }

        public static void ToExcel(Control ctl, string strFileName)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("utf-8");
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            ctl.Page.EnableViewState = false;
            new CultureInfo("ZH-CN", true);
            StringWriter writer = new StringWriter();
            HtmlTextWriter writer2 = new HtmlTextWriter(writer);
            ctl.RenderControl(writer2);
            HttpContext.Current.Response.Write(writer.ToString());
            HttpContext.Current.Response.End();
            writer2.Flush();
            writer2.Close();
            writer.Flush();
            writer.Close();
        }

        public static void ToExcel2(Control ctl, string strFileName)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            ctl.Page.EnableViewState = false;
            new CultureInfo("ZH-CN", true);
            StringWriter writer = new StringWriter();
            HtmlTextWriter writer2 = new HtmlTextWriter(writer);
            ctl.RenderControl(writer2);
            HttpContext.Current.Response.Write(writer.ToString());
            HttpContext.Current.Response.End();
            writer2.Flush();
            writer2.Close();
            writer.Flush();
            writer.Close();
        }
    }
}

